<%@page import="model.bean.Account"%>
<%@page import="common.Privilege"%><jsp:include page="common/header.jsp"></jsp:include>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<style type="text/css">
.form-control {
	width: 70%;
	margin-left: 20%;
}
</style>
<script type="text/javascript">
function checkValid() {
		var message = "";
		var newpass = frAdd.elements["password"].value;
		var rnewpass = frAdd.elements["rpassword"].value;
		if(newpass != rnewpass){
			message += "- Mật khẩu nhập lại không đúng";
		}
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frAdd.email.value)))  
		{  
		   message += "</br>" + "- Nhập lại email";
		}    
		var x = document.forms["frAdd"]["phone"].value;
		if (isNaN(x))
		{
			message += "</br>" + "- Nhập lại số điện thoại";
		}
		if( message != ""){
			document.getElementById('err').innerHTML = message;
			return false;
		}else{
			return true;
		}
}
</script>
<div id="page-wrapper">
<%
	Account account = (Account) session.getAttribute("account");
	Account acc = null;
	if(account == null){
	    response.sendRedirect("../CheckPrivilegeServlet");
	}else{
	    if(account.getPrivilege() != Privilege.ADMIN){%>
	    	<jsp:include page="error.jsp"></jsp:include>
		<%} else {
		    acc = (Account ) request.getAttribute("acc");
		    if(acc != null){%>
				<form name= "frEdit"  action="${pageContext.request.contextPath}/ManageAccountServlet?action=updateAccount" method="POST" 
					onsubmit="return checkValid();">
					<div class="panel panel-default" style="width: 90%; margin: 0 auto; background: none;">
						<div class="panel-heading" style="font-size: 2.0em">Chỉnh sửa tài khoản</div>
							<center>
								<label id="err" style="color: red;"> ${errMessage} </label>
							</center>
							<div class="panel-body" style = "padding-left: 20%;" >
							<div class="form-group">
								<label>Tên tài khoản </label> <input class="form-control" id = "accountName" name = "accountName"
									type="text" required="required" value="<%= acc.getNameAccount() %>" >
							</div>
							<div class="form-group">
								<label>Mật khẩu: </label> <input class="form-control" id = "password" name = "password"
									type="password" required="required" value="<%= acc.getPassword() %>">
							</div>
							<div class="form-group">
								<label>Nhập lại mật khẩu: </label> <input class="form-control" id = "rpassword" name = "rpassword"
									type="password" value="<%= acc.getPassword() %>">
							</div>
							<div class="form-group">
								<label>Chức vụ: </label> <select class="form-control" id = "privilege" name = "privilege">
									<option value="1" <% if (acc.getPrivilege() == 1) out.print("selected"); %> >Quản trị</option>
									<option value="2" <% if (acc.getPrivilege() == 2) out.print("selected"); %> >Quản lý Tour</option>
									<option value="3" <% if (acc.getPrivilege() == 3) out.print("selected"); %> >Quản lý dịch vụ</option>
									<option value="4" <% if (acc.getPrivilege() == 4) out.print("selected"); %> >Quản lý tin tức</option>
									<option value="5" <% if (acc.getPrivilege() == 5) out.print("selected"); %> >Tiếp cận khách hàng</option>
								</select>
							</div>
							<fieldset>
								<legend>Thông tin cá nhân:</legend>
								<div class="form-group">
									<label>Họ tên: </label> <input class="form-control" type="text" id = "fullName" name = "fullName"
										required="required" value="<%= acc.getName() %>">
								</div>
								<div class="form-group">
									<label>Giới tính: </label> <select class="form-control" id = "gender" name = "gender">
										<option value="false" <% if (acc.isSex()) out.print("selected"); %> >Nữ</option>
										<option value="true" <% if (!acc.isSex()) out.print("selected"); %> >Nam</option>
									</select>
								</div>
								<div class="form-group">
									<label>Ngày sinh: </label> <input class="form-control" type="date" id = "dob" name = "dob" value="<%= acc.getDateOfBirth() %>">
								</div>
								<div class="form-group">
									<label>Email: </label> <input class="form-control" type="text" id = "email" name = "email" value="<%= acc.getEmail() %>">
								</div>
								<div class="form-group">
									<label>Phone: </label> <input class="form-control" type="text" id = "phone" name = "phone" value="<%= acc.getPhoneNumber() %>">
								</div>
								<div class="form-group">
									<label>Ảnh đại diện </label> <input class="form-control" type="file" id = "avatar" name = "avatar"> 
								</div>
							</fieldset>
							<div style="float: right; margin-right: 100px;">
								<button type="submit" class="btn btn-success btn-sm">Cập nhập</button>
							</div>
						</div>
					</div>
				</form>
	   <%} else 
	   {%>
    		<center><label id = "err" style = "color: red; font-size: 25px;"> Lỗi nặng lắm, cần quay lại để xử lý gấp  </label> </center>
	   <%}
	   }
	   }%>
</div>
<jsp:include page="common/footer.jsp"></jsp:include>